﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CanDichVu.Db;
using CanDichVu.Service.Iservice;

namespace CanDichVU.Areas.Admin.Controllers
{
    //[Authorize]
    public class Ad_TinhthanhController : Controller
    {
        private readonly ITinhThanh _ITinhThanh;
        private readonly IQuanHuyen _IQuanHuyen;

        public Ad_TinhthanhController(ITinhThanh iTinhThanh, IQuanHuyen iQuanHuyen)
        {
            this._ITinhThanh = iTinhThanh;
            this._IQuanHuyen = iQuanHuyen;
        }

        //
        // GET: /Admin/Ad_Tinhthanh/

        public ActionResult Index_TinhThanh()
        {
            return View();
        }

        public ActionResult _List()
        {
            var _listEntity = _ITinhThanh.getList();
            var jsonResult = Json(_listEntity, JsonRequestBehavior.AllowGet);
            jsonResult.MaxJsonLength = int.MaxValue;
            return jsonResult;
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult AddTinhThanh()
        {
            try
            {
                var Name = Request["models[0][Name]"];
                var Meta_Title = Request["models[0][Meta_Title]"];
                var Meta_Des = Request["models[0][Meta_Des]"];
                var Hot = Request["models[0][Hot]"];
                var stt = Request["models[0][stt]"];
                var Icon = Request["models[0][Icon]"];
                var Ma_QG = Request["models[0][Ma_QG]"];
                var Ma_Vung = Request["models[0][Ma_Vung]"];
                var Meta_KeyWork = Request["models[0][Meta_KeyWork]"];

                TinhThanh tt = new TinhThanh();
                tt.Id = 0;
                tt.Name = Name;
                tt.Meta_Title = Meta_Title;
                tt.Meta_Des = Meta_Des;
                tt.Hot = Boolean.Parse(Hot);
                tt.stt = int.Parse(stt);
                tt.Icon = Icon;
                tt.Ma_QG = Ma_QG;
                tt.Ma_Vung = Ma_Vung;
                tt.NgayTao = DateTime.Now;
                tt.Meta_KeyWork = Meta_KeyWork;

                _ITinhThanh.addorUpdate(tt);

                return Json(true, JsonRequestBehavior.AllowGet);
            }
            catch { return Json(false, JsonRequestBehavior.AllowGet); }
        }


        [HttpPost]
        [ValidateInput(false)]
        public ActionResult UpdateTinhThanh()
        {
            try
            {
                var Id = Request["models[0][Id]"];
                var Name = Request["models[0][Name]"];
                var Meta_Title = Request["models[0][Meta_Title]"];
                var Meta_Des = Request["models[0][Meta_Des]"];
                var Hot = Request["models[0][Hot]"];
                var stt = Request["models[0][stt]"];
                var Icon = Request["models[0][Icon]"];
                var Ma_QG = Request["models[0][Ma_QG]"];
                var Ma_Vung = Request["models[0][Ma_Vung]"];
                var Meta_KeyWork = Request["models[0][Meta_KeyWork]"];

                TinhThanh tt = _ITinhThanh.GetById(int.Parse(Id));
                tt.Name = Name;
                tt.Meta_Title = Meta_Title;
                tt.Meta_Des = Meta_Des;
                tt.Hot = Boolean.Parse(Hot);
                tt.stt = int.Parse(stt);
                tt.Icon = Icon;
                tt.Ma_QG = Ma_QG;
                tt.Ma_Vung = Ma_Vung;
                //tt.NgayTao = DateTime.Now;
                tt.Meta_KeyWork = Meta_KeyWork;

                _ITinhThanh.addorUpdate(tt);

                return Json(true, JsonRequestBehavior.AllowGet);
            }
            catch { return Json(false, JsonRequestBehavior.AllowGet); }
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult DeleteTinhThanh()
        {
            try
            {
                var MA = Request["models[0][Id]"];
                _ITinhThanh.detele(int.Parse(MA));

                return Json(true, JsonRequestBehavior.AllowGet);
            }
            catch { return Json(false, JsonRequestBehavior.AllowGet); }
        }

        public ActionResult QuanhuyenList(int tt)
        {
            var _listEntity = _IQuanHuyen.getList(tt);
            var jsonResult = Json(_listEntity, JsonRequestBehavior.AllowGet);
            jsonResult.MaxJsonLength = int.MaxValue;
            return jsonResult;
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult AddQuanhuyen(string tinhthanhid)
        {
            try
            {
                var Name = Request["models[0][Name]"];
                //var TinhthanhId = Request["models[0][TinhthanhId]"];
                var Meta_Title = Request["models[0][Meta_Title]"];
                var Meta_Des = Request["models[0][Meta_Des]"];
                var Hot = Request["models[0][Hot]"];
                var stt = Request["models[0][stt]"];
                var Icon = Request["models[0][Icon]"];
                var Ma_QG = Request["models[0][Ma_QG]"];
                var Meta_KeyWork = Request["models[0][Meta_KeyWork]"];

                QuanHuyen tt = new QuanHuyen();
                tt.Id = 0;
                tt.Name = Name;
                tt.Meta_Title = Meta_Title;
                tt.Meta_Des = Meta_Des;
                tt.Hot = Boolean.Parse(Hot);
                tt.stt = int.Parse(stt);
                tt.Icon = Icon;
                tt.TinhthanhId = int.Parse(tinhthanhid); ;
                tt.NgayTao = DateTime.Now;
                tt.Meta_KeyWork = Meta_KeyWork;

                _IQuanHuyen.addorUpdate(tt);

                return Json(true, JsonRequestBehavior.AllowGet);
            }
            catch { return Json(false, JsonRequestBehavior.AllowGet); }
        }

        
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult UpdateQuanhuyen(string tinhthanhid)
        {
            try
            {
                var Name = Request["models[0][Name]"];
                var Id = Request["models[0][Id]"];
                var Meta_Title = Request["models[0][Meta_Title]"];
                var Meta_Des = Request["models[0][Meta_Des]"];
                var Hot = Request["models[0][Hot]"];
                var stt = Request["models[0][stt]"];
                var Icon = Request["models[0][Icon]"];
                var Ma_QG = Request["models[0][Ma_QG]"];
                var Meta_KeyWork = Request["models[0][Meta_KeyWork]"];

                QuanHuyen tt = new QuanHuyen();
                tt.Id = int.Parse(Id);
                tt.Name = Name;
                tt.Meta_Title = Meta_Title;
                tt.Meta_Des = Meta_Des;
                tt.Hot = Boolean.Parse(Hot);
                tt.stt = int.Parse(stt);
                tt.Icon = Icon;
                tt.TinhthanhId = int.Parse(tinhthanhid); ;
                tt.NgayTao = DateTime.Now;
                tt.Meta_KeyWork = Meta_KeyWork;

                _IQuanHuyen.addorUpdate(tt);

                return Json(true, JsonRequestBehavior.AllowGet);
            }
            catch { return Json(false, JsonRequestBehavior.AllowGet); }
        }

    }
}
